<template>
  <div class="login-page">
    <van-nav-bar
      title="账号登录"
      left-arrow
      @click-left="$router.push('/register')"
    />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        placeholder="请输入账号"
        :rules="[
          { required: true, message: '请填写用户名' },
          { pattern: /^\w{5,}$/, message: '用户名至少包含5个字符' },
        ]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        placeholder="请输入密码"
        :rules="[
          { required: true, message: '请填写密码' },
          { pattern: /^\w{6,}$/, message: '密码至少包含6个字符' },
        ]"
      />
      <div style="margin: 16px" color="#fff">
        <van-button block type="info" native-type="submit" color="#1cb676"
          >登录</van-button
        >
      </div>
    </van-form>
    <router-link class="link" to="/register">还没有账号,去注册~</router-link>
  </div>
</template>

<script>
import { login } from "@/api/user";
import { setToken } from "@/utils/storage";
export default {
  name: "login-page",
  data() {
    return {
      username: "",
      password: "",
    };
  },
  methods: {
    async onSubmit(values) {
      const res = await login(values);
      setToken(res.body.token);
      this.$toast.success("登录成功");
      this.$router.push("/profile");
    },
  },
};
</script>

<style lang="less" scoped>
.van-nav-bar {
  background-color: #1cb676;
}
/deep/.van-nav-bar .van-icon {
  color: #fff;
}
/deep/.van-ellipsis {
  font-size: 18px;
  color: #fff;
}
.van-button {
  font-size: 18px;
}
.link {
  color: #6e6e6e;
  font-size: 12px;
  padding-right: 20px;
  text-align: center;
  display: block;
}
</style>
